package org.ywf.web.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.ywf.common.entity.RequestData;
import org.ywf.common.entity.ResponseData;
import org.ywf.common.ipaddress.IpAddressUtil;
import org.ywf.common.ipaddress.IpUtils;
import org.ywf.common.request.NewsReq;
import org.ywf.common.service.BaseService;
import org.ywf.framework.sftp.SftpFileService;
import org.ywf.web.client.CarMapper;
import org.ywf.web.client.OrderMapper;
import org.ywf.web.client.UserMapper;
import org.ywf.web.entity.User;

import javax.annotation.Resource;
import java.io.File;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class UserService extends BaseService {


    @Resource
    private UserMapper userMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private CarMapper carMapper;

    @Resource
    private SftpFileService sftpFileService;

    public ResponseData<List> printTest(RequestData<NewsReq> newsReq) {
        String ip = ipUtil.getIp(request);
        String addr = ipUtil.getAddr(ip);
        log.info("IP地址：{}，IP信息：{}",ip,addr);

        log.info("ip:"+ IpUtils.getIpAddr(request));
        log.info("ip:"+ IpUtils.getIpVoByRequest(request));
        log.info("局域网地址：{}",IpAddressUtil.getLocalHostIP());
        log.info("地址信息：{}",IpAddressUtil.getIpAddress(request));

        ResponseData<List> responseData = null;//userMapper.selectById(newsReq);
        System.out.println("responseData:"+responseData);
        return responseData;
    }

    public ResponseData<List> getJoinList(User user) {
        ResponseData<List> responseData = new ResponseData();
        carMapper.getCarInfo();
        return responseData;
    }

    public ResponseData<List> getHostInfo() {
        String ip = ipUtil.getIp(request);
        String addr = ipUtil.getAddr(ip);
        log.info("IP地址：{}，IP信息：{}",ip,addr);

        log.info("ip外网:"+ IpUtils.getIpAddr(request));
        log.info("ip:"+ IpUtils.getIpVoByRequest(request));
        log.info("局域网地址：{}",IpAddressUtil.getLocalHostIP());
        log.info("地址信息：{}",IpAddressUtil.getIpAddress(request));
        log.info("地址信息：{}",ipUtil.getGDIPLocationCityInfo("https://restapi.amap.com/v3/ip",ip));

        ResponseData<List> responseData = null;//userMapper.selectById(newsReq);
        System.out.println("responseData:"+responseData);
        return responseData;
    }

    public List<File> sftpDownload(){
        try {
            return sftpFileService.downloadFiles(this.ftpDownloadPath, (filename) -> (
                    filename.endsWith(".txt") || filename.endsWith(".jar")
                    || filename.endsWith(".zip") || filename.endsWith(".rar")
                    || filename.endsWith(".json")));
        }catch (Exception e){
            log.error("下载文件失败 {}",e.getMessage());
        }
        return Collections.EMPTY_LIST;
    }

    /**
     * 单个文件上传
     * @date 2023/2/11 11:33
     *
     * @param fileName 文件名
     */
    public Boolean sftpUpload(String fileName){
        boolean res = false;
        try {
            //String filePath = "D:\\JavaWork\\config\\icbc/upload\\"+fileName;
            String filePath = this.localUploadPath + fileName;
             log.info("上传文件的路径为：{}", filePath);
            File file = new File(filePath);
            res = sftpFileService.uploadFile(this.ftpDownloadPath, filePath, file);
        }catch (Exception e){
            log.error("上传文件失败 {}",e.getMessage());
        }
        return res;
    }


}
